package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityTimeConversionBinding;
import stark.common.basic.event.EventStatProxy;

public class TimeConversionActivity extends BaseAc<ActivityTimeConversionBinding> {

    private Dialog myTimeUnitDialog;
    private WheelView wheelView;
    private List<String> listUnit = new ArrayList<>();//时间单位
    private int type;
    private int basePos = 3, resultPos = 3;


    @Override
    protected int onCreate() {
        return R.layout.activity_time_conversion;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivTimeConversionBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvBaseUnit.setOnClickListener(this);
        mDataBinding.tvResultUnit.setOnClickListener(this);
        mDataBinding.tvConvert.setOnClickListener(this);
        mDataBinding.tvReset.setOnClickListener(this);

        mDataBinding.etBaseValue.setInputType(InputType.TYPE_CLASS_NUMBER);

        //选择时间单位弹框
        selTimeUnitDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvBaseUnit://初始单位
                type = 0;
                myTimeUnitDialog.show();
                break;
            case R.id.tvResultUnit://换算单位
                type = 1;
                myTimeUnitDialog.show();
                break;
            case R.id.tvConvert://转换
                convert();
                break;
            case R.id.tvReset://重置
                mDataBinding.etBaseValue.setText("");
                mDataBinding.tvResultValue.setText("");
                basePos = 3;
                resultPos = 3;
                mDataBinding.tvBaseUnit.setText(listUnit.get(basePos));
                mDataBinding.tvResultUnit.setText(listUnit.get(resultPos));
                break;
            //选择时间单位弹框
            case R.id.tvDialogTimeUnitCancel://取消
                myTimeUnitDialog.dismiss();
                break;
            case R.id.tvDialogTimeUnitRight://确定
                myTimeUnitDialog.dismiss();
                if (type == 0) {
                    basePos = wheelView.getCurrentItem();
                    mDataBinding.tvBaseUnit.setText(listUnit.get(basePos));
                } else if (type == 1) {
                    resultPos = wheelView.getCurrentItem();
                    mDataBinding.tvResultUnit.setText(listUnit.get(resultPos));
                }
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvBaseUnit.setText(listUnit.get(basePos));
        mDataBinding.tvResultUnit.setText(listUnit.get(resultPos));
    }

    private void convert() {
        String base = mDataBinding.etBaseValue.getText().toString();
        if (TextUtils.isEmpty(base)) {
            return;
        }
        long baseNum = 0;
        if (basePos == 0) {
            baseNum = Long.parseLong(base) * 1000 * 60 * 60 * 24 * 365;
        } else if (basePos == 1) {
            baseNum = Long.parseLong(base) * 1000 * 60 * 60 * 24 * 30;
        } else if (basePos == 2) {
            baseNum = Long.parseLong(base) * 1000 * 60 * 60 * 24;
        } else if (basePos == 3) {
            baseNum = Long.parseLong(base) * 1000 * 60 * 60;
        }
        double resultNum = 0;
        if (resultPos == 0) {
            resultNum = baseNum / 1000f / 60 / 60 / 24 / 365;
        } else if (resultPos == 1) {
            resultNum = baseNum / 1000f / 60 / 60 / 24 / 30;
        } else if (resultPos == 2) {
            resultNum = baseNum / 1000f / 60 / 60 / 24;
        } else if (resultPos == 3) {
            resultNum = baseNum / 1000f / 60 / 60;
        }
        mDataBinding.tvResultValue.setText(String.valueOf(resultNum));
    }

    //选择时间单位弹框
    private void selTimeUnitDialog() {
        myTimeUnitDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_time_unit, null);
        myTimeUnitDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myTimeUnitDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogTimeUnitCancel);
        TextView dialogRight = inflate.findViewById(R.id.tvDialogTimeUnitRight);
        wheelView = inflate.findViewById(R.id.wheel_view1);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
        wheelView.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView.setTextColorCenter(Color.parseColor("#0095FF"));
        wheelView.setLineSpacingMultiplier(2.5f);
        wheelView.setDividerType(WheelView.DividerType.WRAP);
        wheelView.setCurrentItem(0);
        wheelView.setItemsVisibleCount(4);
        wheelView.setCyclic(false);
        wheelView.setTextSize(18);
        //设置滚轮数据

        listUnit.add(getString(R.string.unit_year));
        listUnit.add(getString(R.string.unit_month));
        listUnit.add(getString(R.string.unit_day));
        listUnit.add(getString(R.string.add_time_hour));
        WheelAdapter adapter1 = new ArrayWheelAdapter(listUnit);
        wheelView.setAdapter(adapter1);
    }

}